/******************************************************************************
 * SAGE - Scalable Adaptive Graphics Environment
 *
 * Module: sageAudio.h
 * Author : Hyejung Hur
 * 
 * Copyright (C) 2004 Electronic Visualization Laboratory,
 * University of Illinois at Chicago
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either Version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License along
 * with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Direct questions, comments etc about SAGE to bijeong@evl.uic.edu or 
 * http://www.evl.uic.edu/cavern/forum/
 *
 ***************************************************************************************************************************/

#ifndef _SAGE_AUDIO_H
#define _SAGE_AUDIO_H

// portaudio header file
#include <portaudio.h>

//#ifdef linux
#ifdef RAW1394 
#include <libiec61883/iec61883.h>
#include <sys/select.h>
#endif

#include "sageAudioModule.h"
#include "audioCircBuf.h"
#include "sageBase.h"

class sageAudio {
public:
	/** audio mode
	*/
	enum {AUDIO_PLAY, AUDIO_PAUSE, AUDIO_STOP};
public:
	sageAudio();
	~sageAudio();

	/** set parameters for setting audio up and assign pointer of circular buffer  
	*/
	int init( int id, sageAudioConfig &conf, sageAudioMode mode, audioCircBuf *buf );

	/** open audio stream in play mode or capture mode
	*/
	virtual int	openStream();
	
	/** close audio stream
	*/
	virtual int	closeStream();
	
	int play();
	int pause();
	int stop();

	/** test audio input/output device
	*/
	void testDevices();
	
	int getID();
		
	/** callback function for play audio stream
	*/
	static int playCallback( const void *inputBuffer, void *outputBuffer,
                         unsigned long framesPerBuffer,
                         const PaStreamCallbackTimeInfo* timeInfo,
                         PaStreamCallbackFlags statusFlags,
                         void *userData );

	/** callback function for capture audio stream
	*/
	static int recordCallback( const void *inputBuffer, void *outputBuffer,
                         unsigned long framesPerBuffer,
                         const PaStreamCallbackTimeInfo* timeInfo,
                         PaStreamCallbackFlags statusFlags,
                         void *userData );	
//#ifdef linux
#ifdef RAW1394 

	static void* audioThread(void *args);
	
	/** callback function for capture firewire audio stream
	*/
	static int recordFWCallback(iec61883_amdtp_t amdtp, char *data, 
						int nsamples, unsigned int dbc, 
						unsigned int dropped, void *callback_data);
#endif				
	
protected:
	int ID; 
	
	/** circular buffer for saving audio stream block
	*/
	audioCircBuf* buffer;
	
	/** audio sample format : float, short, char, unsigned char
	*/
	sageSampleFmt sampleFmt;
	
	/** numbers of audio channel
	*/
	int  channels;
	
	/** object for audio streaming : portaudio object
	*/
	PaStream*	audioStream;

	/** parameters for audio streaming
	*/
	PaStreamParameters*	audioParameters;

	/** audio mode : play, capture
	*/
	sageAudioMode audioMode;
	
	/** sound card device number : 0 is default device number
	*/
	int deviceNum;
	
	/** according to audio sample format, byte per sample is different
	*/
	int bytesPerSample; 
	
	/** audio sampling rate : default is 44100
	* standard sampling rate : 8000.0, 9600.0, 11025.0, 12000.0, 16000.0, 22050.0, 24000.0, 32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 192000.0
	*/
	long samplingRate;
	
	/** frames per buffer : default is 1024
	*/
	int  framePerBuffer;

	/** play flag : play, pause, stop
	*/
	int	playFlag;	
	
//#ifdef linux
#ifdef RAW1394 
	raw1394handle_t handle;
	iec61883_amdtp_t amdtp;
#endif
	
	/** latency
	*/
	long minLatency;
	long maxLatency;

}; 

#endif
